<?php

    /**
     * @name 站点类
     * @copyright tcsos inc
     * @version cooperation v1.0
     */
    class site extends cooperation {        


        private $site_split;    //站点分割号
        private $view_def;      //视图默认访问        
        private $rewrite_no_ext;           //不允许被访问的文件后缀        


        //站点配置
        public  $site_config;    

        //数据配置
        public  $data_config;

        /**
         * 构造函数
         * 
         */
        public function __construct(){
            //加载站点配置文件
            $this->load_site_config();

            //站点分割号
            $this->site_split = $this->site_config['site']['site_split'];

            //视图默认访问
            $this->view_def = $this->site_config['site']['view_def'];

            //不允许访问的后缀
            $this->rewrite_no_ext = $this->site_config['rewrite']['no_ext'];
        }

        /**
         * 析构函数
         * 
         */
        public function __destruct(){

        }

        /**         
         * 载入站点配置         
         * @return void
         * 
         */          
        private function load_site_config() {
            $config = $this->_config();

            $setpath = $this->_root() . '/source/settings.php';
            if($config['runtime']['cache']) {
                $setpath = $this->get_datapath('cache/runtime/settings');
                if(!is_file($setpath)) {
                    $settings = file_get_contents($this->_root() . '/source/settings.php');
                    $settings = $this->strip_whitespace($settings);
                    $this->_file()->write_content($settings, $setpath);                
                }            
            }

            //数据配置
            $this->data_config = require_once $setpath;

            $datarows = null;
            $file_obj = $this->_file();

            //读取配置缓存文件，如果不存在就进行生成            
            if(!$datarows = $file_obj->get_file_content($this->get_datapath('config'))) {
                $datarows = $this->_mysql(0)->find('select k,v from config');
                $ary_hash = array();
                foreach($datarows as $val) {
                    if(isset($ary_hash[$val['k']])) {
                        continue;
                    }
                    $ary_hash[$val['k']] = $val['v'];
                }

                $datarows = serialize($ary_hash);            

                //写入缓存
                if($config['runtime']['cache']) {
                    $file_obj->wirte_txt_line($datarows , 'config.php' , $this->_root() . '/data');
                }
            }            

            $datarows = unserialize($datarows);        
            $this->site_config = &$this->_config();
            $this->site_config['site'] = $datarows;                    
        }

        /**         
         * 将伪路径重写成真实路径 
         * http://www.cooperation.com/index.php?v=index/index
         * http://www.cooperation.com/index.php?h=index/index
         * 
         * @param bool  $type   true表示VIEW  false表示HANDLE
         * @return string   如果路径是存在，返回全路径
         */
        public function get_rewrite($type = true) {
            $rewrite_k = $type ? 'v' : 'h';
            $conf_rewrite = $type ? 'view' : 'handle';
            cooperation::$is_view = false;

            //如果是视图，有默认值
            if($type) {
                $view_def = $this->view_def;
            }

            //路由参数值
            $rewrite_v =  request($rewrite_k, true, false, false, $view_def);   
            if($rewrite_v=='index.php')$rewrite_v=$view_def;                                 
            
            //去掉无效的地址
            preg_match("/({$this->rewrite_no_ext})$/i", $rewrite_v, $matchs);
            if($matchs) {
                exit;
            }            

            //unset($_GET[$rewrite_k]);                        

            //如果默认分割号不是/进行转换
            if($this->site_split != '/') {
                $rewrite_v = str_replace($this->site_split, '/', $rewrite_v);
            }

            //获取文件全路径
            if($type) {
                //View
                
                //目录，默认是根目录
                $dir = '';

                if($this->site_config['rewrite']['is_resolve']) {
                    //支持泛域名                    
                    $prefix_ary = explode('.', $_SERVER['HTTP_HOST']);                                                        
                    if($prefix_ary[0] != 'www') {
                        $_GET['prefix'] = $prefix_ary[0];
                        foreach($this->site_config['rewrite']['resolve'] as $k => $v) {
                            if($k == '*' || $k == $_GET['prefix']) {
                                $dir = $v . '/';
                                break;
                            }
                        }                        
                    }
                }

                $file = $this->get_viewpath($dir . $rewrite_v);
		
            } else {
                //Handle
                $file = explode('/', $rewrite_v);
                $file[0] = $this->get_handlepath($file[0]);
            }

            if($file && !is_array($file)) {
                cooperation::$is_view = true;
            }
         
            return $file;
        }

        /**
         * 自动加载OBJECT
         * 
         * @author zhouqi 2012-8-8 AM 10:35:41
         * @param mixed $object_name
         */
        static function load_object($filename) {

            if(class_exists($filename)) {
                return;
            }

            $filename = cooperation::_root() . "/source/object/{$filename}.php";
            if (!is_file($filename)) {
                return;
            }

            return include $filename;
        }
    }        

    $_site = new site();    

    //自动加载
    spl_autoload_register(array('site', 'load_object'));
?>
